package cn.yj.frame.config;

import cn.yj.frame.annotation.RpcService;
import cn.yj.register.AbstractRegisterConfig;
import cn.yj.register.RegisterConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

/**
 * @author 永健
 * @since 2022-01-18 16:03
 */
public class RpcAnnotationBean implements BeanPostProcessor {

    /**
     * bean初始化好后，暴露出去，将其注册到注册中心
     *
     * @param bean
     * @param beanName
     *
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        RpcService service = bean.getClass().getAnnotation(RpcService.class);
        if (service != null) {
            // 暴露接口
            RegisterConfig.export(bean, beanName);
        }
        return bean;
    }
}
